package com.cans.lightning.business.lowcode.manager.form.bean;

import com.cans.lightning.business.core.bean.BaseBean;
import com.cans.lightning.business.core.utils.ContextUtil;
import com.cans.lightning.business.lowcode.dto.AppTableFieldDto;
import com.cans.lightning.business.lowcode.enums.AppIndexEnum;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author cans
 * @date 2023/11/28
 **/
@Getter
@Setter
public class AppFormBean extends BaseBean {

    public static final String CURRENT_VIEW_ID = "currentViewId";

    public AppFormBean() {
    }

    public AppFormBean(String formId) {
        this.setId(formId);
    }

    /**
     * 表单名称
     */
    private String formName;

    /**
     * 应用ID
     */
    private String appId;
    /**
     * 字段游标
     */
    private Integer fieldIndex = 1;

    /**
     * 主表
     */
    private MainTableBean mainTableBean;

    /**
     * 明细表
     */
    private List<SonTableBean> sonTableList;

    /**
     * 视图信息
     */
    private List<ViewBean> viewList;

    /**
     * 当前的视图
     */
    private ViewBean viewBean = new ViewBean();

    /**
     * 表单权限
     */
    private List<FormAuthBean> formAuthList;

    /**
     * key 数据库字段名
     */
    private Map<String, FormFieldBean> fieldMap;

    /**
     * 当前展示的信息
     * 1. 当前展示的视图 currentViewId
     */
    private Map<String, String> currentInfo = Maps.newHashMap();

    @JsonIgnore
    public String getCurrentViewId() {
        return currentInfo.get(AppFormBean.CURRENT_VIEW_ID);
    }

    public String updateCurrentViewId(String viewId) {
        return currentInfo.put(AppFormBean.CURRENT_VIEW_ID,viewId);
    }

    @JsonIgnore
    public String getNextFiendName() {
        Integer fieldIndex = this.getFieldIndex();
        if (fieldIndex == null) {
            fieldIndex = 1;
        }
        this.setFieldIndex(fieldIndex + 1);
        return AppIndexEnum.FIELD.getKey() + Strings.padStart(String.valueOf(fieldIndex), 5, '0');
    }

    @JsonIgnore
    public List<TableBean> getAllTableBean() {
        List<TableBean> tableBeanLists = Lists.newArrayList();
        tableBeanLists.add(this.getMainTableBean());
        tableBeanLists.addAll(this.getSonTableList());
        return tableBeanLists;
    }


    /**
     * 表中添加字段
     *
     * @param fieldDto 字段
     * @return
     */
    public FormFieldBean addField(AppTableFieldDto fieldDto) {
        Map<String, TableBean> tableBeanMap = this.getAllTableBean().stream().collect(Collectors.toMap(TableBean::getId, Function.identity()));
        TableBean tableBean = tableBeanMap.get(fieldDto.getTableId());
        List<FormFieldBean> fieldList = tableBean.getFieldList();
        FormFieldBean formFieldBean = new FormFieldBean().create(fieldDto);
        formFieldBean.setId(ContextUtil.nextID());
        formFieldBean.setDbFieldName(this.getNextFiendName());
        fieldList.add(formFieldBean);
        return formFieldBean;
    }

}
